package ru.tpu.AAD.MalakhovaZaripov.RMI;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import ru.tpu.AAD.MalakhovaZaripov.DAO.ComputerShopDAO;
import ru.tpu.AAD.MalakhovaZaripov.Entities.Customer;

public class ComputerShopImpl extends UnicastRemoteObject implements
		ComputerShop {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3364953678164729505L;
	
	ComputerShopDAO dao = new ComputerShopDAO();;
    SessionFactory sessionFactory = dao.getSessionFactory();

	protected ComputerShopImpl() throws RemoteException {
		super();
	}

	
	@Override
	public List<Customer> getAllCustomers() throws RemoteException {
		Session session = sessionFactory.openSession();
	    ArrayList<Customer> customers =(ArrayList<Customer>) session.createCriteria(Customer.class).list();
	    session.close();
	    return customers;
	}

	@Override
	public Customer getCustomer(Integer id) throws RemoteException {
		Session session = sessionFactory.openSession();
		Customer customer = (Customer)session.get(Customer.class,id);
	    session.close();
	    
		return customer;
	}


	@Override
	public int deleteCustomer(Customer oldCustomer) throws RemoteException {
		try
		{
			dao.deleteCustomer(oldCustomer);
			return 0;
		}
		catch (Exception ex)
		{
			System.out.println(ex.getMessage());
			return -1;
		}
	}


	@Override
	public int insertCustomer(Customer newCustomer) throws RemoteException {
		try
		{
			dao.insertCustomer(newCustomer);
			return 0;
		}
		catch (Exception ex)
		{
			System.out.println(ex.getMessage());
			return -1;
		}
		
	}


	@Override
	public int updateCustomer(Customer oldCustomer) throws RemoteException {
		try
		{
			dao.updateCustomer(oldCustomer);
			return 0;
		}
		catch (Exception ex)
		{
			System.out.println(ex.getMessage());
			return -1;
		}
		
	}

	
	
}
